import java.io.*;
import java.util.Random;

public class Main {

    static final int maxObjects = 21;
    static final int minObjectsToTake = 1;
    static final int maxObjectsToTake = 5;

    public static int getObjects() throws IOException {
        int objectsToTake = 0;
        do {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            objectsToTake = Integer.parseInt(bufferedReader.readLine());
            if (objectsToTake < minObjectsToTake || objectsToTake > maxObjectsToTake) {
                System.out.print("Not valid input. Try again: ");
            }
        } while (objectsToTake < minObjectsToTake || objectsToTake > maxObjectsToTake);
        return objectsToTake;
    }

    public static int AnalyzeThis(int residueOfObjects) {
        int CompMoveObjects = 0;
        if (residueOfObjects % (minObjectsToTake + maxObjectsToTake) == 0) {
            Random random = new Random();
            CompMoveObjects = random.nextInt(maxObjectsToTake - 1) + 1;
        } else {
            CompMoveObjects = residueOfObjects % (minObjectsToTake + maxObjectsToTake);
        }
        return CompMoveObjects;
    }

    public static void main(String[] args) throws Exception {
        String newGame = "Y";
        Random random = new Random();
        int move = random.nextInt(2) + 1;
        do {
            int residueOfObjects = maxObjects;
            System.out.println("We have " + maxObjects + " objects");
            System.out.println("You can take from " + minObjectsToTake + " to " + maxObjectsToTake + " objects.");
            do {
                if (move == 1) {
                    System.out.print("Your move now. Please make it:");
                    int takenObjects = getObjects();
                    residueOfObjects -= takenObjects;
                    System.out.println("change " + residueOfObjects);
                    move = 2;
                } else {
                    System.out.print("Computer have move now. Move is: ");
                    int CompMoveObjects = AnalyzeThis(residueOfObjects);
                    residueOfObjects -= CompMoveObjects;
                    System.out.print(CompMoveObjects);
                    System.out.println(" change " + residueOfObjects);
                    move = 1;
                }
            } while (residueOfObjects > 0);
            if (move == 1) {
                System.out.print("Computer winning. Want more (y/n)? ");
            } else {
                System.out.print("You are winning. Want more (y/n)? ");
            }
            do {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
                newGame = bufferedReader.readLine();
            } while (newGame.toUpperCase() == "Y" || newGame.toUpperCase() == "N");
            System.out.print("");
        } while (newGame.toUpperCase() == "N");
    }
}